package ua.com.jpy.services.auth.mongo.customer.password;

import java.security.NoSuchAlgorithmException;

import jonelo.jacksum.JacksumAPI;
import jonelo.jacksum.algorithm.AbstractChecksum;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.security.authentication.encoding.PasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * @author LSD25
 *
 */
@Service
public class PasswordEncoderService implements PasswordEncoder {
	
	private static final Log log = LogFactory.getLog(PasswordEncoderService.class);
	
	private static final String WHIRLPOOL = "whirlpool";
	
	@Override
	public String encodePassword(String rawPass, Object salt) {
		String encodedPassword = null;
		AbstractChecksum checksum = null; 
		try {
			checksum = JacksumAPI.getChecksumInstance(WHIRLPOOL);
		} catch (NoSuchAlgorithmException exc) {
			log.error(null, exc);
			return null;
		}
		checksum.update(rawPass.getBytes());
		encodedPassword = checksum.getFormattedValue();
		return encodedPassword;
	}

	@Override
	public boolean isPasswordValid(String encPass, String rawPass, Object salt) {
		String encodeRawPass = encodePassword(rawPass, salt);
		return encodeRawPass.equals(encPass) ? true : false;
	}

}
